<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addPermision({type:1,pid:''})">添加权限</el-button>
        </div>
        <el-table border :data="feachList" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button type="text" @click="addPermision({type:2,pid:row.id})">添加</el-button>
              <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
              <el-button type="text" @click="delList(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 弹窗 -->
    <Add-permission
      :visible="visible"
      :cur-id="curId"
      :type="type"
      :pid="pid"
      @close-dialog="closeDialog"
      @updata-list="hGetPermissionList"
      @removecurID="removecurID"
    />
  </div>
</template>

<script>
import { getPermissionList, delPermission } from '@/api/permisson'
import transTree from '@/utils/transTree'
import AddPermission from './component/add-permission.vue'
export default {
  name: 'Permission',
  components: {
    AddPermission
  },
  data() {
    return {
      feachList: [],
      visible: false,
      type: null,
      pid: '',
      curId: ''
    }
  },
  // 更新页面
  mounted() {
    this.hGetPermissionList()
  },
  methods: {
    // 获取数据
    async hGetPermissionList() {
      const res = await getPermissionList()
      this.feachList = transTree(res)
    },
    // 点击添加
    addPermision({ type, pid }) {
      this.visible = true
      console.log(type)
      console.log(pid)
      this.type = type
      this.pid = pid
    },
    // 关闭弹窗
    closeDialog() {
      this.visible = false
    },
    // 点击编辑
    async editPermission(id) {
      this.curId = id
      // console.log(this.curId)
      this.visible = true
    },
    // 点击删除
    async delList(id) {
      this.$confirm('确定要删除该权限吗?', '温馨提示').then(async() => {
        await delPermission(id)
        this.hGetPermissionList()
        this.$message.success('删除成功')
      }).catch(() => {
        console.log('取消')
      })
    },
    // 关闭弹窗id清除
    removecurID() {
      this.curId = ''
    }
  }
}

</script>

<style lang="scss" scoped>

</style>
